package practiceProjects.extractTitlesFromHtmlElement;

/**
 * @Project ：learn_java
 * @File ：TitleExtractor2.java
 * @IDE ：IntelliJ IDEA
 * @Author ：自由如风间
 * @Date ：2025/4/8 17:08
 * @Description ：根据正则表达式从html元素中提取标题，从文件中读取，豆包提供代码，
 * 这个方案相比一更简洁高效
 */

import java.io.IOException;
import java.nio.file.Files;
import java.nio.file.Paths;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class TitleExtractor2 {
    private static final Pattern TITLE_PATTERN = Pattern.compile(
            "<div\\s+title\\s*=\\s*\"([^\"]+)\"", // 匹配div标签的title属性
            Pattern.CASE_INSENSITIVE | Pattern.UNICODE_CASE
    );

    public static void main(String[] args) {
        String filePath = "G:\\codes\\learn_java\\src\\practiceProjects\\提取尚硅谷springboot网课课程标题\\sourceHtmlElement.txt"; // 输入文件路径

        try {
            String htmlContent = new String(Files.readAllBytes(Paths.get(filePath)));
            Matcher matcher = TITLE_PATTERN.matcher(htmlContent);

            while (matcher.find()) {
                String title = matcher.group(0);
                System.out.println("提取到标题: " + title);
            }

        } catch (IOException e) {
            System.err.println("文件读取失败: " + e.getMessage());
        }
    }
}
